package org.korosoft.findbugs.aggregator;

import java.io.File;

public final class Bug {
    private boolean canEdit = true;
    private int hashcode;

    private String type;
    private String priority;
    private String category;
    private String message;
    private int startLine;
    private int endLine;
    private File sourceFile;
    private String className;
    private String tag;

    public Bug() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.type = type;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.priority = priority;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.category = category;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.message = message;
    }

    public int getStartLine() {
        return startLine;
    }

    public void setStartLine(int startLine) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.startLine = startLine;
    }

    public int getEndLine() {
        return endLine;
    }

    public void setEndLine(int endLine) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.endLine = endLine;
    }

    public File getSourceFile() {
        return sourceFile;
    }

    public void setSourceFile(File sourceFile) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.sourceFile = sourceFile;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        if (!canEdit) {
            throw new IllegalStateException("This object might be in map or set");
        }
        this.className = className;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Bug bug = (Bug) o;

        if (endLine != bug.endLine) return false;
        if (startLine != bug.startLine) return false;
        if (category != null ? !category.equals(bug.category) : bug.category != null) return false;
        if (className != null ? !className.equals(bug.className) : bug.className != null) return false;
        if (message != null ? !message.equals(bug.message) : bug.message != null) return false;
        if (priority != null ? !priority.equals(bug.priority) : bug.priority != null) return false;
        if (sourceFile != null ? !sourceFile.equals(bug.sourceFile) : bug.sourceFile != null) return false;
        if (type != null ? !type.equals(bug.type) : bug.type != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        canEdit = false;
        if (hashcode == 0) {
            int result = type != null ? type.hashCode() : 0;
            result = 31 * result + (priority != null ? priority.hashCode() : 0);
            result = 31 * result + (category != null ? category.hashCode() : 0);
            result = 31 * result + (message != null ? message.hashCode() : 0);
            result = 31 * result + startLine;
            result = 31 * result + endLine;
            result = 31 * result + (sourceFile != null ? sourceFile.hashCode() : 0);
            result = 31 * result + (className != null ? className.hashCode() : 0);
            hashcode = result;
        }
        return hashcode;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getTextualLines() {
        if (startLine == 0) {
            return "";
        }
        return ":" + (startLine == endLine ? Integer.toString(startLine) : (Integer.toString(startLine) + "-" + Integer.toString(endLine)));
    }

    public String getEnumLines() {
        if (startLine == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(startLine);
        for (int i = startLine + 1; i <= endLine; i++) {
            sb.append(",");
            sb.append(i);
        }
        return sb.toString();
    }
}